package com.test.miniosc;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

	WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl("http://m.oschina.net/");
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});


	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				doubleClickToExit();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}


	long exitTime = 0;

	private void doubleClickToExit() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			String applicationName = getResources().getString(
					R.string.app_name);
			String msg = "再按一次返回键退出" + applicationName;
			Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			finish();
		}
	}

//
//	private void goHome() {
//		Intent home = new Intent(Intent.ACTION_MAIN);
//		home.addCategory(Intent.CATEGORY_HOME);
//		startActivity(home);
//	}
}
